package com.letv.android.client.episode.utils;

import android.text.TextUtils;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;

public class EpisodeUtils {
	/*
	 * 过期天数计算因子
	 */
    public static final long OVERDUE_TIME_IN_SECS = 86400000; // 24 * 60 * 60 * 1000

//	public static String getString(int id) {
//		return BaseApplication.getInstance().getString(id);
//	}
//
//	public static String getString(int id, Object... objects) {
//		return BaseApplication.getInstance().getString(id, objects);
//	}
	
	/**
	 * 格式化时间字符串
	 * 
	 * @param timeMs
	 *            毫秒
	 * @return 返回格式00:00
	 */
	public static String stringForTimeNoHour(long timeMs) {

		StringBuilder formatBuilder = new StringBuilder();
		Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());

		try {
			if(timeMs>=0) {
				int totalSeconds = (int) (timeMs / 1000);
	
				int seconds = totalSeconds % 60;
				int minutes = totalSeconds / 60;
	
				formatBuilder.setLength(0);
	
				return formatter.format("%02d:%02d", minutes, seconds).toString();
			} else {
				return "00:00";
			}
		} finally {
			formatter.close();
		}
	}

	/**
	 * 格式化时间字符串
	 * 
	 * @param timeMs
	 *            毫秒
	 * @return 返回格式00:00:00
	 */
	public static String stringForTime(long timeMs) {

		StringBuilder formatBuilder = new StringBuilder();
		Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());

		try {
			int totalSeconds = (int) (timeMs / 1000);

			int seconds = totalSeconds % 60;
			int minutes = (totalSeconds / 60) % 60;
			int hours = totalSeconds / 3600;

			formatBuilder.setLength(0);

			return formatter.format("%02d:%02d:%02d", hours, minutes, seconds).toString();
		} finally {
			formatter.close();
		}
	}
	
	/**
     * 播放数转换
     * @param playCount
     * @return
     */
    public static String getPlayCountsToStr(long playCount){
        if(playCount<10000) //小于10000
        {
            if(playCount<1000){
                return playCount+"";
            }else if(playCount>=1000 && playCount<=9999){
                String aa = playCount/1000+"";
                String bb = playCount%1000+"";
                if(bb.length()<3){
                    bb = "0"+bb;
                }
                return aa+","+bb;
            }
            return "";
        }else if(playCount >=10000 && playCount <100000000){    //万以上  亿一下
            DecimalFormat df=new DecimalFormat(".#");
            String st=df.format(playCount*1.0/10000)+"万";
            return st;
        }else{   //亿以上
            DecimalFormat df=new DecimalFormat(".#");
            String st=df.format(playCount*1.0/100000000)+"亿";
            return st;
        }
    }
    
    /**
     * 根据数量，计算行数(起点1)
     * */
    public static int calculateRows(int index, int rowSize) {
        int a = index % rowSize;
        int b = index / rowSize;

        return a == 0 ? b : b + 1;
    }
    
    public static String formatData(String date) {
        if (!TextUtils.isEmpty(date)) {
            if(date.length()==8){
                String year = date.substring(0,4);
                String month = date.substring(4,6);
                String day = date.substring(6,8);
                return year+"-"+month+"-"+day;
            }
        }
        return null;
    }

    /**
     * 转换时间
     *
     * 00:43:23 或者 01:33:21格式的转换成  xx:xx 或 xxx:xx格式
     * */
    public static String string2FormatStringTime(String time){
        String formatTime="";

        String []times = time.split(":");
        if(times!=null && times.length==3){
            if(times[0].equals("00")){
                formatTime = times[1]+":"+times[2];
            }else{
                int s1 = Integer.parseInt(times[0].substring(0,1));
                int s2 = Integer.parseInt(times[0].substring(1));
                int add ;
                if(s1 == 0){
                    add = s2 * 60;
                }else{
                    add = s1 * 10 * 60;
                }
                int s3 = Integer.parseInt(times[1].substring(0,1));
                int s4 = Integer.parseInt(times[1].substring(1));
                String newTime = String.valueOf(add+s3*10+s4);
                formatTime = newTime + ":" + times[2];
            }
        }

        return formatTime;
    }
    
    public static long stringToLong2(String str) {
		long i = 0;
		try{
			i = Long.parseLong(str);
		}catch(Exception e){
			e.printStackTrace();
		}
		return i;
	}

    public static int stringToLong(String time) {
        String[] times = time.split(":");
        if (times.length == 3) {
            int hour = Integer.parseInt(times[0]);
            int minute = Integer.parseInt(times[1]);
            int second = Integer.parseInt(times[2]);
            return hour * 60 * 60 + minute * 60 + second;
        } else if (times.length == 2) {
            int minute = Integer.parseInt(times[0]);
            int second = Integer.parseInt(times[1]);
            return minute * 60 + second;
        } else if (times.length == 1) {
        	if (!TextUtils.isEmpty(times[0])) {
        		return Integer.parseInt(times[0]);
        	}
        }
        return 0;
    }
    
    public static int getMinusDaysBetweenTwoDate(long endTime, long startTime) {
        Date endDate = new Date(endTime);
        Date beginDate = new Date(startTime);

        Calendar c = Calendar.getInstance();
        c.setTime(endDate);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        long endMilliSec = c.getTimeInMillis();

        c.setTime(beginDate);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        long startMilliSec = c.getTimeInMillis();

        return (int)((endMilliSec - startMilliSec) / OVERDUE_TIME_IN_SECS); 	// 1000 * 60 * 60 * 24
    }
    
    /**
	 * 评论点赞数业务逻辑
	 * 预期结果：1）<10000，直接显示数字
		2）>=10000,显示X.X万，1.0万显示1万
		3）>=1亿，显示X.X亿
	 */
	
    public static String toCommentLikeCountText(int playCount){
        if(playCount <= 0){
            return "";
        }else if(playCount < 10000){
            DecimalFormat df = new DecimalFormat("#,###");
            String m = df.format(playCount);
            return  m + "";
        }else if(playCount <= 100000000){
            double count = playCount / 10000.0;
            DecimalFormat df = new DecimalFormat("#,###.0");
            String m = df.format(count);
            if (m!=null&&m.endsWith(".0")) {
				m = m.substring(0, m.length()-2);
			}
            return m + "万";
        }else{//亿次以上
            double count = playCount / 100000000.0;
            DecimalFormat df = new DecimalFormat("#,###.0");
            String m = df.format(count);
            if (m!=null&&m.endsWith(".0")) {
				m = m.substring(0, m.length()-2);
			}
            return m + "亿";
        }
    }
	
//	public static OnCheckedChangeListener getOnCheckedChangeListener(
//			int normalColor, int selectColor) {
//		mNormalColor = normalColor;
//		mSelectColor = selectColor;
//		return mOnCheckedChangeListener;
//	}
//
//	static int mNormalColor, mSelectColor;
//
//	static OnCheckedChangeListener mOnCheckedChangeListener = new OnCheckedChangeListener() {
//
//		@Override
//		public void onCheckedChanged(CompoundButton buttonView,
//				boolean isChecked) {
//			if (isChecked) {
//				((TextView) buttonView).setTextColor(mSelectColor);
//				buttonView.setBackgroundResource(R.drawable.letv_textbtn_bkg_active);
//
//			} else {
//				((TextView) buttonView).setTextColor(mNormalColor);
//				buttonView.setBackgroundResource(R.drawable.letv_textbtn_bkg);
//			}
//
//		}
//	};
//
//	
//
//	 
//	
//    /**
//     * 设置颜色选择器  
//     * @param checkBox
//     * @param onColorSelecterListener
//     * @author yangn
//     */
//	public static void setOnColorSelecterListener(CompoundButton checkBox, OnColorSelecterListener onColorSelecterListener) {
//		if(null==checkBox||null==onColorSelecterListener){
//			return;
//		}
//		checkBox.setOnTouchListener(onColorSelecterListener);
//		//checkBox.setOnCheckedChangeListener(null);
//		checkBox.setTextColor(onColorSelecterListener.getNormalColor());
//		checkBox.setBackgroundResource(onColorSelecterListener.getNormalBacakgroundDrawableId());
//	}
//	
//	/**
//	 * 
//	 * @param checkBox
//	 * @param onColorSelecterListener
//	 */
//	public static void setOnColorCheckAndSelecterListener(CompoundButton compoundButton,
//			OnColorSelecterListener onColorSelecterListener) {
//		if(null==compoundButton||null==onColorSelecterListener){
//			return;
//		}
//		compoundButton.setOnTouchListener(onColorSelecterListener);
//		compoundButton.setOnCheckedChangeListener(onColorSelecterListener);
//		compoundButton.setTextColor(onColorSelecterListener.getNormalColor());
//		compoundButton.setBackgroundResource(onColorSelecterListener.getNormalBacakgroundDrawableId());
//
//	}
	
	

}
